<template>
  <div class="editTask" id="editTask">
    <div class="task-paper">
      <el-form class="task-form" ref="form" :model="FormData" label-width="80px">
      <!-- 试卷名 -->
        <h1 class="task-title">
          {{FormData.title}}
          {{'（'+FormData.totalScore+'分满）'}}</h1>
        <el-form-item prop="title">
          <el-input
          type="textarea"
          :autosize="{minRows:1}"
          style="width: 50%;"
          class="task-el-edit-title task-el-input"
          v-model="FormData.title"
          placeholder="请输入内容"/>
        </el-form-item>
        <div class="task-h2" v-if="FormData.singleData.length">
              <!-- '每题'+FormData.singleData[0].score+'分，'+ -->
          <h2>{{capitalFigures[
              FormData.singleData.length?0:-1
            ]}}. 单项选择{{
            '('+
            '共'+FormData.singleData.length+'题，共'+getSingleScoreADD+
            '分)'
            }}
            <!-- +'共'+FormData.singleData[0].score*FormData.singleData.length+'分'+ -->
          </h2>
        </div>
        <div id="single-choice" class="task-single-choice2" >
          <singelChoice :FormData="FormData" @getSingleAnswer="getSingleAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.multipleData.length">
          <h2>{{capitalFigures[
              FormData.singleData.length?1:0
            ]}}. 多项选择
            <span v-if="FormData.multipleData.length">
            {{'(每题'+FormData.multipleData[0].score+
            '分，'+'共'+FormData.multipleData.length+'题，' +
            '共'+FormData.multipleData[0].score*FormData.multipleData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="multiple-choice" class="task-multiple-choice" >
          <multipleChoice :FormData="FormData" @getMultipleAnswer="getMultipleAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.judgeData.length">
          <h2>{{capitalFigures[
              (FormData.singleData.length?1:0) + (FormData.multipleData.length?1:0)
            ]}}. 判断题
            <span v-if="FormData.judgeData.length">
            {{'(每题'+FormData.judgeData[0].score+
            '分，'+'共'+FormData.judgeData.length+'题，' +
            '共'+FormData.judgeData[0].score*FormData.judgeData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="judge" class="task-judge">
          <judge :FormData="FormData" @getJudgeAnswer="getJudgeAnswer"/>
        </div>
        <div class="task-h2" v-if="FormData.programmingData.length">
          <h2>{{capitalFigures[
              (FormData.judgeData.length?1:0) + (FormData.singleData.length?1:0) + (FormData.multipleData.length?1:0)
            ]}}. 编程题
            <span v-if="FormData.programmingData.length">
            {{'(每题'+FormData.programmingData[0].score+
            '分，'+'共'+FormData.programmingData.length+'题，' +
            '共'+FormData.programmingData[0].score*FormData.programmingData.length+'分)'}}
            </span>
          </h2>
        </div>
        <div id="program" class="task-judge">
          <programming :FormData="FormData"/>
        </div>
        <div class="paperBottom"></div>
      </el-form>
    </div>
  <!-- 工具箱 -->
    <toolBox :FormData="FormData" :key="time"/>
  </div>
</template>

<script>
import {Paper} from '../javascript/paper.js'
import singelChoice from './questionBank/singleChoice.vue'
import multipleChoice from './questionBank/multipleChoice.vue'
import judge from './questionBank/judge.vue'
import toolBox from './toolBox.vue'
// import dialogScore from '../components/dialog/dialog-score.vue'
import programming from './questionBank/programming.vue'
export default {
  name: 'task-edit',
  components: {singelChoice, toolBox, multipleChoice, judge, programming},
  props: ['taskData'],
  data () {
    return {
      s: 0,
      m: 0,
      j: 0,
      p: 0,
      time: new Date().getTime(),
      dialogScore_open: false,
      scoreModel: {
        setScoreDialog: false,
        type: '',
        typeZH: '',
        score: null
      },
      paper: '',
      getSingleAnswer: {},
      getMultipleAnswer: {},
      getJudgeAnswer: {},
      // 选择题选项
      choiceList: ['A', 'B', 'C', 'D'],
      capitalFigures: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三'],
      // 试卷题型
      FormData: {},
      // 试卷答案
      FormDataAnswer: {
        sigleData: []
      },
      h_index: 0
    }
  },
  async created () {
    this.paper = new Paper()
    this.FormData = {
      sectionId: this.taskData.sectionId,
      homework_id: this.taskData.paper.id,
      title: this.taskData.paper.title,
      totalScore: 0,
      // createPeople: this.taskData.paper.created_by.username,
      singleData: [],
      multipleData: [],
      judgeData: [],
      programmingData: []
    }
    await this.taskData.paperDetail.forEach(e => {
      if (e.selection_problem_type === 'single') {
        if (Array.isArray(e.answer)) {
          e.answer = e.answer[0]
        }
        this.FormData.singleData.push(e)
      } else if (e.selection_problem_type === 'multiple') {
        this.FormData.multipleData.push(JSON.parse(JSON.stringify(e)))
      } else if (e.selection_problem_type === 'judge') {
        if (Array.isArray(e.answer)) {
          e.answer = e.answer[0]
        }
        this.FormData.judgeData.push(JSON.parse(JSON.stringify(e)))
      }
    })
    await this.taskData.paperDetailProblems.forEach(e => {
      this.FormData.programmingData.push(e)
    })
    // 计算作业成绩
    if (this.FormData.singleData.length) {
      await this.FormData.singleData.forEach(_ => {
        this.s += _.score
      })
    }
    if (this.FormData.multipleData.length) {
      await this.FormData.multipleData.forEach(_ => {
        this.m += _.score
      })
    }
    if (this.FormData.judgeData.length) {
      await this.FormData.judgeData.forEach(_ => {
        this.j += _.score
      })
    }
    if (this.FormData.programmingData.length) {
      await this.FormData.programmingData.forEach(_ => {
        this.p += _.score
      })
    }
    this.FormData.totalScore = await (this.s + this.m + this.j + this.p)
    this.FormData.required = true
    this.elementId = this.taskData.paper.id
  },
  methods: {
    // 设置分数
    setScore () {
      if (!this.FormData[this.scoreModel.type].length) {
        return this.$message({
          message: '请先添加题型再设置分数',
          type: 'warning'
        })
      }
      this.FormData[this.scoreModel.type].forEach((item, index) => {
        item.score = this.scoreModel.score
      })
      this.scoreModel.setScoreDialog = false
      this.$message({
        message: '成功设置分数',
        type: 'success'
      })
    }
  },
  computed: {
    getSingleScoreADD: function () {
      let add = 0
      this.FormData.singleData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getMultipleScoreADD: function () {
      let add = 0
      this.FormData.multipleData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getJudgeScoreADD: function () {
      let add = 0
      this.FormData.judgeData.forEach(item => {
        add += Number(item.score)
      })
      return add
    },
    getProgrammingScoreADD: function () {
      let add = 0
      this.FormData.programmingData.forEach(item => {
        add += Number(item.score)
      })
      return add
    }
  }
}
</script>

<style lang="less">
.task-h2{
  width: 95%;
  height: 50px;
  text-align: left;
  margin:0 30px;
}
@import url('./task-edit.less');
.paperBottom {
  height: 30px;
}
</style>
